package com.example.dict.GUI;

import com.example.dict.utils.Dictionary.Operations.AvlTree;
import com.example.dict.utils.Dictionary.dictionaryOperations;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.*;

import static com.example.dict.data.staticFilterData.WORD_CHECKER;


public class translateGUI extends JFrame {

    dictionaryOperations dictionary = AvlTree.acquire();

    JTextPane input, output;
    JMenuItem createRecordItem, deleteRecordItem;

    String[] optionsToChoose = {"       英文 —> 中文","       中文 —> 英文"};
    JComboBox<String> comboBox = new JComboBox<>(optionsToChoose);
    Color gray = Color.getHSBColor(0, 0, 0.933f);
    Color fontGray = Color.getHSBColor(0, 0, 0.7922f);

    int width = 512;
    int height = 600;

    public translateGUI() {
        super("翻译");

        JPanel panel = new JPanel();
        Border round = new LineBorder(gray, 25, true);
        JMenuBar menuBar = new JMenuBar();
        JMenu menu = new JMenu("功能");
        createRecordItem = new JMenuItem("新增");
        deleteRecordItem = new JMenuItem("删除");
        input = new JTextPane();
        output = new JTextPane();


        JLabel version = new JLabel("版本: 壹点零");
        JLabel creator = new JLabel("作者: wey");

        panel.setBackground(Color.WHITE);
        panel.setLayout(null);

        menu.setFont(new Font("楷体", Font.BOLD, 20));
        createRecordItem.setFont(new Font("楷体", Font.BOLD, 20));
        deleteRecordItem.setFont(new Font("楷体", Font.BOLD, 20));
        input.setFont(new Font("楷体", Font.BOLD, 20));
        output.setFont(new Font("宋体", Font.BOLD, 20));
        version.setFont(new Font("宋体", Font.BOLD, 20));
        creator.setFont(new Font("宋体", Font.BOLD, 20));
        input.setForeground(fontGray);
        input.setText("请输入要翻译的单词");
        output.setText("翻译区域");

//        output.setText("这里是一些示例文本。\n这里是第二行。\n第三行，更多的内容...\n等等...");
        output.setBackground(gray);
        output.setEditable(false);
        // 创建 JScrollPane 包含 JTextPane
        JScrollPane scrollPane = new JScrollPane(output);
        scrollPane.setBounds(width/4, height*2/4, 256,212); // 设置滚动面板的位置和大小

        menuBar.setBackground(gray);
        input.setBackground(gray);


        input.setBorder(round);
        scrollPane.setBorder(round);

        panel.setBounds(0, 0, width, height);
        menuBar.setBounds(0, 0, 1024, 30);
        comboBox.setBounds(55, 70, 250, 35);
        input.setBounds(width/4, height/4, 256, 100);
//        output.setBounds(width/4, height*2/4, 256,212);
        version.setBounds(320, 50, 300, 20);
        creator.setBounds(320, 90, 300, 20);

        setLayout(null);
        menu.add(createRecordItem);
        menu.add(deleteRecordItem);
        menuBar.add(menu);
        panel.add(menuBar);
        panel.add(comboBox);
        panel.add(input);
//        panel.add(output);
        panel.add(version);
        panel.add(creator);
        panel.add(scrollPane);
        add(panel);

        int x = ((Toolkit.getDefaultToolkit().getScreenSize()).width - width) / 2;
        int y = ((Toolkit.getDefaultToolkit().getScreenSize()).height - height) / 2;
        setLocation(x, y);
        setSize(width, height);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        setVisible(true);


    }



    public void init() {

        input.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                input.setText("");
                output.setText("");
            }
        });

        input.addKeyListener(new KeyAdapter() {


            @Override
            public void keyReleased(KeyEvent e) {

                String selectedOption = (String) comboBox.getSelectedItem();
                String word = input.getText();
                if (selectedOption.trim().equals("英文 —> 中文")) {
                    if (!word.matches(WORD_CHECKER)) {
                        output.setText("翻译区域");
                        return;
                    }

                    String translation = dictionary.translate(word.toLowerCase());

                    output.setText((null == translation) ? "单词还未收录，您可以添加" : translation);
                } else {
                    String ans = dictionary.translateC2E(word);
                    output.setText((null == ans) ? "单词还未收录，您可以添加" : ans);
                }

            }
        });

        createRecordItem.addActionListener(e -> new addGUI());

        deleteRecordItem.addActionListener(e -> new deleteGUI());
    }
}